package com.wxl.common.util

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.get
import com.wxl.common.R
import java.lang.reflect.Field

class LayoutHelper {
    
    companion object {


        /**
         * 添加头部布局，带水滴效果
         */
        fun addTitleTopLayout(layout:ViewGroup) : View {
            val titleTopLayout = View.inflate(layout.context, R.layout.view_title_top_layout,null)
            val parent = LinearLayout(layout.context)
            parent.orientation = LinearLayout.VERTICAL
            parent.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
            parent.setBackgroundResource(R.mipmap.bg_window)
            parent.addView(titleTopLayout)
            parent.addView(layout)
            return parent
        }


        /**
         * 添加头部布局，带纯色效果
         */
        fun addTitleBgLayout(layout:ViewGroup) : View {
            val titleTopLayout = View.inflate(layout.context, R.layout.view_title_bg_layout,null)
            val parent = LinearLayout(layout.context)
            parent.orientation = LinearLayout.VERTICAL
            parent.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
            parent.setBackgroundResource(R.mipmap.bg_window)
            parent.addView(titleTopLayout)
            parent.addView(layout)
            return parent
        }

        /**
         * 获得雷管列表头部的view宽度
         */
        fun getDetonatorTitleViewsWidth(layout:ViewGroup,callback:ObservableUtil.Companion.OnNextCallback<ArrayList<Int>>){
            layout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{
                override fun onGlobalLayout() {
                    layout.viewTreeObserver.removeOnGlobalLayoutListener(this)
                    val d = arrayListOf<Int>()
                    for (i in 0 until layout.childCount){
                        if(layout.get(i) is AppCompatTextView) {
                            d.add(layout.get(i).width)
                        }
                    }
                    callback.onNext(d)
                }
            })

        }


        /**
         * 反射设置光标颜色  R.drawable.edittext_cursor
         *
         * @param edittextView
         * @param drawable 资源文件
         */
        @SuppressLint("NewApi")
        fun setCursorColor(edittextView: EditText?, drawable: Int) {
            try { //修改光标的颜色（反射）
                val f: Field = TextView::class.java.getDeclaredField("mCursorDrawableRes")
                f.setAccessible(true)
                f.set(edittextView, drawable)
            } catch (e: Exception) {
                //Log.e(TAG,e);
            }
        }
    }
}


